const app = getApp();
import qs from "qs";
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    couponArr: [{
      CouponName: 'weianl红包',
      CouponTypeName: '满30元可用',
      CouponMoney: 5,
      HasItBeenClaimed: false,
    },
    {
      CouponName: 'weianl红包',
      CouponTypeName: '满100元可用',
      CouponMoney: 20,
      HasItBeenClaimed: false,
    },
    {
      CouponName: 'weianl红包',
      CouponTypeName: '满5000元可用',
      CouponMoney: 500,
      HasItBeenClaimed: false,
    },
    {
      CouponName: 'weianl红包',
      CouponTypeName: '满10000元可用',
      CouponMoney: 1000,
      HasItBeenClaimed: true,
    }
    ],
    topImages: '/images/topcoupon.png',
    couponLeftImages: 'http://m.qpic.cn/psc?/V14ZaBeY40XWC8/zkoezU7GGNbZGOF.DPhgQXDCLspq1L1upRR.ZiRnZuFvq1XezxpUwmIc4ky9cr0DEpxn.YXOFA15Y03Wwkk2zJSBTVERFZsf3KTl5vSZorE!/b&bo=lgCWAJYAlgADCSw!&rf=viewer_4',
    closeBtnImages: '/images/close.png',
    // 是否显示优惠劵弹窗
    isShowCouponPopUp: false

  },

  /**
   * 组件的方法列表
   */
  methods: {
    //阻止弹出层滑动事件，空函数，不做任何处理
    onPreventTouchMove: function () {
      return false;
    },
    //打开优惠劵弹窗
    openTheCouponPopUp: function () {
      var that = this;
      setTimeout(() => {
        // 先开启优惠劵弹窗
        that.setData({
          isShowCouponPopUp: true
        })
        // 设置优惠劵弹窗打开动画
        var animation = wx.createAnimation({
          duration: 600,
          timingFunction: 'ease',
        })
        that.animation = animation;
        animation.scale(1).step();
        that.setData({
          animationData: animation.export()
        })
      }, 1000)
    },
    //关闭优惠劵弹窗
    closeTheCouponPopUp: function () {
      // 设置优惠劵弹窗关闭动画
      var animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'ease',
      })
      this.animation = animation;
      animation.scale(0).step();
      this.setData({
        animationData: animation.export(),
      })
      //执行完动画后再关闭
      setTimeout(() => {
        this.setData({
          isShowCouponPopUp: false
        })
      }, 200)
    },
    //领取单个优惠劵
    getCoupons: function (e) {
      app.functions.setLocalStorage("iscoupon",1);
      // console.log(e.currentTarget.dataset.index)
      var index = e.currentTarget.dataset.index;
      var couponArr = this.data.couponArr;
      couponArr[index].HasItBeenClaimed = true;
      this.setData({
        couponArr: couponArr
      })
    },
    //已领取优惠劵
    alreadyReceived: function () {
      wx.showToast({
        title: '已领取，可在卡包查看',
        icon: 'none'
      })
    },
    //领取全部优惠劵
    getAllCoupons: function () {
      app.functions.setLocalStorage("iscoupon",1);
      var couponArr = this.data.couponArr;
      couponArr.forEach((item) => {
        item.HasItBeenClaimed = true;
      })
      this.setData({
        couponArr: couponArr
      })
      wx.showToast({
        title: '领取成功，已放入卡包',
        icon: 'none'
      })
      this.closeTheCouponPopUp();
    },
  }
})
